package cn.visu.security.realm;


import cn.visu.bean.User;
import cn.visu.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 自定义域
 * shiro从域中获取数据 例如  角色 权限等
 * <p>
 * 就是说 securityManager 需要验证用户身份
 * 那么它需要从Realm中拿到相应的用户进行比较
 * 以确定用户身份是否合法.
 * 也需要从Realm中拿到用户对应的角色和权限验证用户是否能操作
 * 可以吧Realm看成安全数据源
 * 本域只自定义添加了用户的角色和权限
 */
public class JdbcRealm extends AuthorizingRealm {
    private static final Logger logger = LoggerFactory.getLogger(JdbcRealm.class);

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("PrincipalCollection: {}", principalCollection);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        String username = principalCollection.getPrimaryPrincipal().toString();
        //加入角色
        authorizationInfo.addRoles(userService.findRoles(username));
        //加入权限
        authorizationInfo.addStringPermissions(userService.findPermissions(username));
        logger.info("AuthorizationInfo: {}", authorizationInfo);
        return authorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        logger.info("AuthenticationToken: {}", token);
        String username = (String) token.getPrincipal();
        User user = userService.getUserByUserName(username);
        if (user == null)
            throw new UnknownAccountException
                    ("没有找到该账号");
        if (user.getStatus() == 0) {
            throw new UnknownAccountException("该账号已被禁用");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUserName(), user.getUserPassword(), getName());
        logger.info("AuthenticationInfo: {}", info);
        return info;
    }

    @Override
    public String getName() {
        return getClass().getName();
    }


    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    public void clearCacheBeforeLogin(String username) {
        getAuthenticationCache().remove(username);
        logger.info("{}:{}", getAuthorizationCache().keys(), getAuthenticationCache().keys());
        logger.info("sessionCache: {}", getCacheManager().getCache("shiro-activeSessionCache").keys());
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    @Override
    protected void doClearCache(PrincipalCollection principals) {
        getAuthorizationCache().remove(getAuthorizationCacheKey(principals));
        getAuthenticationCache().remove(getAuthenticationCacheKey(principals));
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }


}
